<?php
/*
 * 图片上传类, 对传入的文件进行处理, 主要用于图片上传
 * 不对上传的图片进行缩略图处理, 但是限制上传文件的大小
 * 1. 根据模块对其进行命名
 * 2. 限制上传文件的格式
 * 3. 不生成缩略图, 缩略图使用缩略图工具进行生成, 并后期开发缩略图管理对文件进行管理
 * @package    sys
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 8ebb7ccd324e553e7652f8b1d863670c981506bf $
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 使用iframe方法上传的
 * Class PictureUpload
 */
class MultiUpload extends PictureUpload {

	/**
	 * 保存图片
	 * 设置表单信息
	 * @param $fileData
	 * -------------------------------------
	 * [name=>QQ截图20130529115220.png]
	 * [type=>application/octet-stream]
	 * [tmp_name=>D:\wamp\tmp\phpE52E.tmp]
	 * [error=>0]
	 * [size=>6594]
	 * --------------------------------------
	 * @return bool
	 */
	public function save($fileData) {
		//PHP上传失败
		if (parent::save($fileData)) {
			global $DT;
			if (parent::save($fileData)) {
				// 检查是否文件超大. 压缩
				if (PhpThumbFactory::create($this->aimPath)->resize($DT['max_image'])->save($this->aimPath)) {
					$this->url = Picture::url($this->aimPath);
				}
				return true;
			} else {
				$this->setErr(self::ERR_NOT_WRITEABLE);
				return false;
			}
		} else {
			$this->setErr(self::ERR_NOT_WRITEABLE);
			return false;
		}

	}

	public function error($append = '') {
		parent::error($append);
	}

	public function ok() {
		$fileinfo = pathinfo($this->url);
		$return   = array(
			'error'     => 0,
			'url'       => $this->url,
			'basename'  => $fileinfo['basename'],
			'extension' => $fileinfo['extension'] ? 1 : 0,
			'id'        => $fileinfo['filename'],
		);
		exit(json_encode($return));
	}

	// todo
	public static function clearDiff($group, array $new_urls, array $old_urls, $userid) {
		$session_all = self::getSession($group);
		$session_urls = (array) __($session_all)->pluck('url');

		$all_urls = array_merge($session_urls, $old_urls);

		// 需要删除的图片
		foreach ($all_urls as $url) {
			if (in_array($url, $new_urls)) {
				self::clearPictureSession($url);
			} else {
				self::deletePicture($url, $userid);
				self::clearPictureSession($url);
			}
		}
	}


}